import api from '../../utils/api.js'

import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
// pages/addDizhi/addDizhi.js
var app = getApp();
let http = new HTTP()
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        biaoqian: ["家", "公司", "学校"],
        current: 0,
        sex: 0,
        name: "",
        phone: "",
        xxdz: "",
        status: false,
        lon: "",
        lat: "",
        animationAddressMenu: {},
        addressMenuIsShow: false,
        value: [0, 0, 0],
        provinces: [],
        citys: [],
        areas: [],
        areaInfo: '',

        select: 0,
        town: "",
        value1: [0, 0, 0],
        provinces1: [],
        citys1: [],
        imgUrl: config.imgUrl,
        tinaxie: false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    bindfollow: function(e) {
        var animation = wx.createAnimation({
            duration: 1500,
            timingFunction: 'linear',
        })
        this.animation = animation
        animation.opacity(0).step()
        this.setData({
            animationData: animation.export()
        })
        setTimeout(function() {
            this.setData({
                hidden: true
            })
        }.bind(this), 2000)
    },

    onLoad: function(options) {
        var address = wx.getStorageSync("address").address
        this.setData({
            address
        })
    },
    change() {
        var status = !this.data.status
        this.setData({
            status: status
        })
    },
    no() {
        wx.showToast({
            title: '为确保您正常下单，请前去个人中心修改',
            icon: "none",
        })
    },
    // 保存
    save(e) {
        var city = this.data.city
        var value = this.data.value
        var value1 = this.data.value1
        var status = ""
        if (this.data.status) {
            status = 1
        } else {
            status = 0
        }
        var num = 0;
        if (this.data.xxdz.length > this.data.areaInfo.length) {
            num++
        } else {
            wx.showToast({
                title: '请完善您的信息(详细地址)',
                icon: 'none',
                duration: 1500
            })
        }
        if (this.data.phone) {
            if (!(/^1[345789]\d{9}$/.test(this.data.phone))) {
                wx.showToast({
                    title: '手机号码有误，请重新填写',
                    icon: 'none',
                    duration: 1500
                })
            } else {
                num++
            }
        } else {
            wx.showToast({
                title: '请完善您的信息(手机号)',
                icon: 'none',
                duration: 1500
            })
        }
        if (this.data.name) {
            num++
        } else {
            wx.showToast({
                title: '请完善您的信息(姓名)',
                icon: 'none',
                duration: 1500
            })
        }
        if (num == 3) {
            let qqmapsdk = new QQMapWX({
                key: 'OI5BZ-G3FWJ-A6VFG-FGA5U-AZHAT-2NBVY'
            })
            var address = wx.getStorageSync("address")
            http.request({
                url: api.addAddress,
                method: "POST",
                data: {
                    name: this.data.name,
                    user_id: wx.getStorageSync("user_id"),
                    sex: this.data.sex,
                    mobile: this.data.phone,
                    detail: this.data.xxdz,
                    type: this.data.current,
                    default: 1,
                    address: address.address,
                    lon: address.longitude,
                    lat: address.latitude,
                    hometown_id: 10,
                    city_id: wx.getStorageSync("city_id")
                },
                success: (res) => {
                    console.log(res)
                    if (res.code == 200) {
                        wx.showToast({
                            title: '编辑成功',
                            duration: 1500
                        })
                        var address = wx.getStorageSync("address")
                        address.name = this.data.name
                        address.phone = this.data.phone
                        address.detail = this.data.xxdz
                        wx.setStorageSync("address", address)
                        setTimeout(() => {
                            wx.navigateBack({})
                        }, 1000)
                    } else if (res.code == 1003) {
                        wx.showToast({
                            title: '请不要重复提交',
                            icon: 'none',
                            duration: 1500
                        })
                    } else if (res.data == "detail长度不能小于 5") {
                        wx.showToast({
                            title: "详细地址过短",
                            icon: "none",
                            duration: 1500
                        })
                    }
                }
            })

        }
    },
    radioChange: function(e) {
        this.setData({
            sex: e.detail.value
        })
    },
    name: function(e) {
        this.setData({
            name: e.detail.value
        })
    },
    phone: function(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    xxdz: function(e) {
        var xxdz = e.detail.value
        if (xxdz.indexOf("区") != -1) {
            var xxdz = e.detail.value.split("区")[1]
        }
        this.setData({
            xxdz
        })
    },
    changeIndex(e) {
        this.setData({
            current: e.target.dataset.idx
        })
    },
    changeSex(e) {
        console.log(e.target.dataset.sex)
        this.setData({
            sex: e.target.dataset.sex
        })
    },
    // 点击所在地区弹出选择框
    select: function(e) {
        if (e.target.dataset.select == 1) {
            this.setData({
                select: 1
            })
        } else {
            this.setData({
                select: 2
            })
        }
        // 如果已经显示，不在执行显示动画
        if (this.data.addressMenuIsShow) {
            return false
        } else {
            // 执行显示动画
            this.startAddressAnimation(true)
        }
    },
    // 执行动画
    startAddressAnimation: function(isShow) {
        if (isShow) {
            // vh是用来表示尺寸的单位，高度全屏是100vh
            this.animation.translateY(0 + 'vh').step()
        } else {
            this.animation.translateY(40 + 'vh').step()
        }
        this.setData({
            animationAddressMenu: this.animation.export(),
            addressMenuIsShow: isShow,
        })
    },
    // 点击地区选择取消按钮
    cityCancel: function(e) {
        this.startAddressAnimation(false)
    },
    // 点击地区选择确定按钮
    citySure: function(e) {
        this.setData({
            tianxie: true
        })
        var that = this
        var city = that.data.city
        var value = that.data.value
        var city1 = that.data.city1
        var value1 = that.data.value1
        this.startAddressAnimation(false)
            // 将选择的城市信息显示到输入框
        var areaInfo = that.data.provinces[value[0]].province_name + that.data.citys[value[1]].city_name + that.data.areas[value[2]].county_name
        var town = ""
        var city = that.data.citys1[value1[1]].city_name
        if (city.indexOf("市辖") != -1) {
            town = that.data.provinces1[value1[0]].province_name
        } else {
            town = that.data.provinces1[value1[0]].province_name + city
        }

        if (this.data.select == 1) {
            that.setData({
                areaInfo: areaInfo,
                xxdz: areaInfo,
            })
        } else {
            that.setData({
                town
            })
        }
    },
    // 处理省市县联动逻辑
    cityChange: function(e) {
        if (this.data.select == 1) {
            var value = e.detail.value
            var provinces = this.data.provinces
            var citys = this.data.citys
            var areas = this.data.areas
            var provinceNum = value[0]
            var cityNum = value[1]
            var countyNum = value[2]
                // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
            if (this.data.value[0] != provinceNum) {
                var id = provinces[provinceNum].province_id
                http.request({
                    url: api.city,
                    data: {
                        province_id: id
                    },
                    method: "POSt",
                    success: (res) => {
                        this.setData({
                            value: [provinceNum, 0, 0],
                            citys: res.data
                        })
                        var id = res.data[0]
                        http.request({
                            url: api.county,
                            data: {
                                city_id: res.data[0].city_id
                            },
                            method: "POSt",
                            success: (res) => {
                                this.setData({
                                    areas: res.data
                                })
                            }
                        })
                    }
                })
            } else if (this.data.value[1] != cityNum) {
                // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
                var id = citys[cityNum].city_id
                http.request({
                    url: api.county,
                    data: {
                        city_id: id
                    },
                    method: "POSt",
                    success: (res) => {
                        this.setData({
                            value: [provinceNum, cityNum, 0],
                            areas: res.data
                        })
                    }
                })
            } else {
                // 滑动选择了区
                this.setData({
                    value: [provinceNum, cityNum, countyNum]
                })
            }
        } else {
            var value = e.detail.value
            var provinces = this.data.provinces1
            var citys = this.data.citys1
            var provinceNum = value[0]
            var cityNum = value[1]
                // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
            if (this.data.value1[0] != provinceNum) {
                var id = provinces[provinceNum].province_id
                http.request({
                    url: api.city,
                    data: {
                        province_id: id
                    },
                    method: "POSt",
                    success: (res) => {
                        console.log(res)
                        this.setData({
                            value1: [provinceNum, 0, 0],
                            citys1: res.data
                        })
                    }
                })
            } else if (this.data.value1[1] != cityNum) {
                // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
                this.setData({
                    value1: [provinceNum, cityNum],
                })
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var animation = wx.createAnimation({
            duration: 1500,
            timingFunction: 'linear',
        })
        this.animation = animation
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})